Do("dialog", "datalazyload", function() {
    Cute.Class.namespace("AGL.tweets");
    AGL.tweets.list = function(options) {
        var opt = $.extend({
            element: "",
            methods: "appendTo",
            id: 0,
            start: 0,
            limit: 20,
            climit: 2,
            school_id: 0,
            cate_id: 0,
            isInit: false,
            callback: $.noop
			
        }, options || {});
        opt.isInit && $(opt.element).empty().html('<li class="nodata">主题加载中...</li>');
        Cute.api.get("tweets/lists", {
            id: opt.id,
            start: opt.start,
            limit: opt.limit,
            climit: opt.climit,
            school_id: opt.school_id,
            cate_id: opt.cate_id
        }, function(json) {
            opt.isInit && $(opt.element).empty();
            if (json.status == 1) {
                if (json.data && json.data.length > 0) {
                    $.each(json.data, function(i, item) {
                        var data = {
                            show_more_comment: opt.climit == 0 ? false : true,
                            tweets:item
                        };
                        var tweets = $(Cute.template("TWEETS",data))[opt.methods](opt.element);
                        var new_comment = $(".new_comment",tweets);
                        if(data.show_more_comment){
                            new_comment.out("click",function(e){
                                if(e.target.className.indexOf("comment_link") == -1 && Cute.form.isInputNull($("textarea",new_comment))){
                                    $(this).hide();
                                }
                            },true);
                        }
                    });
                    if(!AGL.tweets.list.event_bind){
                        AGL.tweets.list.event_bind = true;
                        //加载更多
                        $(opt.element).on("click.tweets",".more_comments_link",function(){
                            var _self = this;
                            var _parent = $(this).parent();
                            var _cur_comment_id = parseInt(_parent.next().attr("id").replace("comment-id-",""));
                            Cute.api.get("comment/lists",{
                                tweets_id: $(this).attr("data-tweets-id"),
                                start:_cur_comment_id,
                                limit:10
                            },function(json){
                                if(json.status > 0){
                                    if(json.data == null){
                                        $(_self).hide();
                                    }else{
                                        $.each(json.data,function(i,item){
                                            item.floor = parseInt(_parent.next().find(".comment_floor").text()) - 1;
                                            $(Cute.template("NEW_COMMENT", item)).insertAfter(_parent);
                                        });
                                        if(_parent.find(".collapse").length == 0){
                                            $('<a href="javascript:;" class="collapse">收起↑</a>').appendTo(_parent).click(function(){
                                                _parent.siblings(".comment:lt(" + (_parent.siblings(".comment").length - 2) + ")").remove();
                                                $(this).remove();
                                                $(_self).show();
                                                return false;
                                            });
                                        }
                                        if(json.data.length < 10)
                                            $(_self).hide();
                                    }
                                }
                            });
                            return false;
                        });
                        //若清空内容，则同时清除父级关系
                        $(opt.element).on("keyup.tweets","textarea.comment_content", function(e) {
                            if (e.keyCode == 8 || e.keyCode == 46) {
                                if(Cute.form.isInputNull(this)){
                                    $(this).siblings("[name=parent_id]").val(0);
                                }
                            }
                        });
                        //展开评论框
                        $(opt.element).on("click.tweets","a.comment_link", function(){
                            var _self = this;
                            var tweets_id = $(_self).attr("data-tweets-id");
                            var comment_id = $(_self).attr("data-comment-id");
                            var new_comment = $("#comment-form" + tweets_id);
                            if(comment_id){
                                $("input[name=parent_id]",new_comment).val(comment_id);
                                var str =  "to " + $(".comment_floor","#comment-id-" + comment_id).text() + ": ";
                            }else{
                                $("input[name=parent_id]",new_comment).val(0);
                                var str =  "";
                            }
                            new_comment.slideDown('fast').find("textarea.comment_content").val(str).focus();
                            return false;
                        });
                        //私信
                        $(opt.element).on("click.tweets","a.message_link", function(){
                            var tweets_id = $(this).attr("data-tweets-id");
                            var user_id = $(this).attr("data-user-id");
                            var nickname = $(this).attr("data-nickname");
                            AGL.message.post(user_id, tweets_id, nickname);
                            return false;
                        });
                        //移除
                        $(opt.element).on("click.tweets","a.report_link", function(){
                            var _self = this;
                            var tweets_id = $(_self).attr("data-tweets-id");
                            var comment_id = $(_self).attr("data-comment-id");
                            new Cute.ui.dialog().ajax("我要移除它",{
                                action:"report/dialog",
                                params:{
                                    tweets_id: tweets_id,
                                    comment_id: comment_id
                                },
                                mask: true,
                                buttons: [{
                                    title: '提交',
                                    type: 'main',
                                    close: false,
                                    func: function() {
                                        var obj = this.pannel.find("textarea[name=content]");
                                        if (Cute.form.isInputNull(obj)) {
                                            AGL.common.errorAnimation(obj);
                                            return false;
                                        }
                                        Cute.api.post("report/save", this.pannel.find("form").serialize(), function(json) {
                                            if (json.status > 0) {
                                                this.close();
                                                new Cute.ui.dialog().suggest("请求已发出，我们会及时确认并处理！");
                                            }else{
                                                alert(json.info);
                                            }
                                        }.bind(this));
                                    }
                                }, {
                                    title: '取消',
                                    type: 'cancel',
                                    close: true
                                }]
                            });
                            return false;
                        });
                        //提交评论
                        $(opt.element).on("submit.tweets",".new_comment>form", function(){
                            var _self = this;
                            var _parent = $(_self).parent();
                            var _content = $("textarea.comment_content", _self);
                            if(Cute.form.isInputNull(_content) || _content.val().replace(/to\ \d+F\:/i,"").trim() == ""){
                                AGL.common.errorAnimation(_content);
                                return false;
                            }
                            Cute.api.post("comment/save",$(this).serialize(),function(json){
                                if(json.status > 0){
                                    _content.val("");
                                    json.data.floor = (parseInt(_parent.prev().find(".comment_floor").text()) || 0) + 1;
                                    var comment = $(Cute.template("NEW_COMMENT",json.data)).insertBefore(_parent);
                                    new Cute.ui.dialog().suggest("评论成功！");
                                    comment.hide().slideDown("fast");
                                }else{
                                    new Cute.ui.dialog().notice(json.info);
                                }
                            });
                            return false;
                        });
                        //删除
                        //若有data-comment-id则为删除评论
                        $(opt.element).on('click.tweets', 'a.delete_link', function(){
                            var _self = this;
                            var tweets_id = $(_self).attr("data-tweets-id");
                            var comment_id = $(_self).attr("data-comment-id");
                            Cute.common.confirm("确认要删除吗？", function(){
                                if(comment_id){
                                    Cute.api.get("comment/delete",{
                                        comment_id:comment_id
                                    },function(json){
                                        if(json.status > 0){
                                            $(".comment_body","#comment-id-" + comment_id).fadeOut("normal", function(){
                                                $(this).html("该评论已被删除！").fadeIn("normal");
                                            });
                                            new Cute.ui.dialog().suggest("删除成功！");
                                        }else{
                                            new Cute.ui.dialog().notice(json.info);
                                        }
                                    });
                                }else{
                                    Cute.api.get("tweets/delete",{
                                        tweets_id:tweets_id
                                    },function(json){
                                        if(json.status > 0){
                                            $("#tweets-id-" + tweets_id).slideUp("normal", function(){
                                                $(this).remove();
                                            });
                                            new Cute.ui.dialog().suggest("删除成功！");
                                        }else{
                                            new Cute.ui.dialog().notice(json.info);
                                        }
                                    });
                                }
                            });
                            return false;
                        });
                        //显示被回复评论
                        $(opt.element).on('mouseenter.tweets', 'abbr.comment_object', function(e){
                            clearTimeout(this.timer);
                            var _self = this;
                            this.timer = setTimeout(function(){
                                var parent_id = $(_self).attr("data-parent-id");
                                if(_self.dialog)
                                    _self.dialog.show();
                                else{
                                    Cute.api.get("comment/get",{
                                        comment_id:parent_id
                                    },function(json){
                                        _self.dialog = new Cute.ui.dialog().tooltip("layer", "", {
                                            width: 230,
                                            content: Cute.template("PARENT_COMMENT",json.data),
                                            mask: false,
                                            pos: {
                                                top: function(){
                                                    return $(_self).offset().top - $(this).height() - 8;
                                                },
                                                left: $(_self).offset().left - 54 + $(_self).width()/2
                                            },
                                            open:{
                                                callback: function(){
                                                    this.pannel.on("mouseleave.tweets", function(){
                                                        this.hide();
                                                    }.bind(this));
                                                }
                                            }
                                        });
                                    });
                                }
                            },300);
                        }).on('mouseleave.tweets', 'abbr.comment_object', function(e){
                            clearTimeout(this.timer);
                            this.timer = setTimeout(function(){
                                console.log(e.relatedTarget);
                                if(this.dialog && $(e.relatedTarget).closest(".dialog").length == 0){
                                    this.dialog.hide(); 
                                }
                            }.bind(this), 300);
                        });
                    }
                //Cute.plugin.datalazyload({
                //elements: $("img[dynamic-src]", opt.element),
                //placeholder: Cute.config.RESOURCEURL + "i/public/1x1.gif",
                //threshold: 50
                //});
                }else{
                    if(opt.climit == 0){
                        $('<div class="nodata">该主题不存在或已被删除！</div>')[opt.methods](opt.element);
                    }else{
                        $('<div class="nodata">还有没有任何主题！</div>')[opt.methods](opt.element);
                    }
                }
                opt.callback.call(opt, json);
            }
        });
    };
});